package com.jp.knowledge.my.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.fragment.OrderDetailFragment;
import com.jp.knowledge.my.fragment.OrderDetailScheduleFragment;
import com.jp.knowledge.my.model.OrderModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.TabView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/5.
 */

public class OrderDetailActivity extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener {

    private static final int BUTTON_STATE_ORDER_PAY = 0x01;         // 支付订单
    private static final int BUTTON_STATE_ORDER_CANCEL = 0x02;      // 取消订单
    private static final int BUTTON_STATE_ORDER_REFUNDS = 0x03;     // 退款
    private static final int BUTTON_STATE_ORDER_RECEIPT = 0x04;     // 确认收货

    @ViewInject(R.id.tab_view)
    private TabView tabView;
    @ViewInject(R.id.order_viewpager)
    private ViewPager viewPager;

    @ViewInject(R.id.order_btn_view)
    private LinearLayout orderBtnView;
    @ViewInject(R.id.orderBtn1)
    private TextView btn1;
    @ViewInject(R.id.orderBtn2)
    private TextView btn2;

    private List<Fragment> fragments;
    private int position;
    private List<OrderModel> orderModels;
    private OrderModel orderModel;

    @Override
    protected int getResourceId() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void init() {
        position = getIntent().getIntExtra("position", 0);
        orderModels = (List<OrderModel>) getIntent().getSerializableExtra("data");
        orderModel = orderModels.get(position);

        initTitleBar();
        fragments = new ArrayList<>();
        fragments.add(OrderDetailScheduleFragment.newInstance(orderModel.getOrderId()));
        fragments.add(OrderDetailFragment.newInstance(orderModels, position));
        viewPager.setBackgroundColor(getResources().getColor(R.color.white));
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        tabView.setTabItems("订单进度", "订单详情");
        tabView.setupWithViewPager(viewPager);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn1.setTag(0);
        btn2.setTag(0);
        setOrderBtn();
    }

    private void initTitleBar(){
        topName.setText("订单详情");
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightBtn.setText("我的发票");
        rightBtn.setVisibility(View.VISIBLE);
        rightIcon.setVisibility(View.GONE);
        leftIcon.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
    }

    private void setOrderBtn(){
        int orderState = orderModel.getOrderState();
        int payState = orderModel.getPayState();
        if((orderState == 1) && (payState == 0 || payState == 2)){
            btn1.setText("去支付");
            btn1.setTag(BUTTON_STATE_ORDER_PAY);
            btn2.setText("取消订单");
            btn2.setTag(BUTTON_STATE_ORDER_CANCEL);
        } else if(orderState == 2 && payState == 1){
            btn1.setText("确认收货");
            btn1.setTag(BUTTON_STATE_ORDER_RECEIPT);
            btn2.setVisibility(View.GONE);
        } else if((orderState == 4 || orderState == 5) && (payState == 1)){
            btn1.setText("取消订单");
            btn1.setTag(BUTTON_STATE_ORDER_CANCEL);
            btn2.setVisibility(View.GONE);
        } else if(orderState == 3 && payState == 1){
            btn1.setText("申请退款");
            btn1.setTag(BUTTON_STATE_ORDER_REFUNDS);
            btn2.setVisibility(View.GONE);
        } else{
            orderBtnView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if(v == leftIcon){
            finish();
        } else if(v == rightBtn){

        } else if(v == btn1 || v == btn2){
            int buttonState = (int)v.getTag();
            if (buttonState == BUTTON_STATE_ORDER_PAY){

            } else if(buttonState == BUTTON_STATE_ORDER_CANCEL){
                JsonObject param = new JsonObject();
                param.addProperty("orderId", orderModel.getOrderId());
                HttpHelper.getInstance(mContext).orderReceipt(param, BUTTON_STATE_ORDER_CANCEL, this);
            } else if(buttonState == BUTTON_STATE_ORDER_REFUNDS){

            } else if(buttonState == BUTTON_STATE_ORDER_RECEIPT){
                JsonObject param = new JsonObject();
                param.addProperty("orderId", orderModel.getOrderId());
                HttpHelper.getInstance(mContext).orderReceipt(param, BUTTON_STATE_ORDER_RECEIPT, this);
            }
        }
    }

    @Override
    public void onStart(int requestType) {
        loading.show();
    }
    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
            return;
        }
        if(requestType == BUTTON_STATE_ORDER_PAY){

        } else if(requestType == BUTTON_STATE_ORDER_CANCEL){
            orderModel.setOrderState(3);
        } else if(requestType == BUTTON_STATE_ORDER_REFUNDS){

        } else if(requestType == BUTTON_STATE_ORDER_RECEIPT){
            orderModel.setOrderState(5);
        }
        setOrderBtn();
        ((OrderDetailFragment)fragments.get(1)).updateItemView();
    }

    @Override
    public void finish() {
        setResult(RESULT_OK, getIntent().putExtra("data", orderModel));
        super.finish();
    }
}
